#!/usr/bin/env python

"""X11 color names to Apple color pallete.

Program to convert X11 color names (rgb.txt) to Apple color pallete (.clr).
"""

__author__ = "Ruda Moura <ruda.moura@gmail.com>"
__version__ = "11.2"

import sys

RGB = "/usr/X11/share/X11/rgb.txt"

def rgb2clr(remove_duplicated=False):
    try:
        rgb = open(RGB)
    except:
        print "RGB file '%s' not found" % RGB_TXT
        sys.exit(1)
    entries = []
    seen = {}
    while True:
        line = rgb.readline()
        if not line:
            break
        line = line.strip().split()
        r, g, b = line[0], line[1], line[2]
        name = ' '.join(line[3:])
        if remove_duplicated and seen.has_key((r, g, b)):
            continue
        seen[(r, g, b)] = 1
        entry = 0.0, float(r)/255.0, float(g)/255.0, float(b)/255.0, 1.0, name
        entries.append(entry)
    rgb.close()
    return entries

if __name__ == '__main__':
    remove_dup = False
    for arg in sys.argv[1:]:
        if arg == '--help' or arg == '-h':
            print sys.argv[0], '[-h|--help] [--remove-dup|-r]'
            print
            print __doc__
            sys.exit(0)
        if arg == '--remove-dup' or arg == '-r':
            remove_dup = True
    entries = rgb2clr(remove_dup)
    print len(entries)    
    for entry in entries:
        print "%d %1.4f %1.4f %1.4f %1.4f %s" % entry
